<template>
    <el-card shadow="never" style="border: 0;">
        <yun-form
            :data="row"
            @submit="onSubmit"
            ref="yunform"
            :columns="columns">
            <template #default>
                {:token_field()}
            </template>
            <template #limit_day_number="{rows}">
                <el-form-item label="使用数量限制:">
                    <el-row style="width: 100%">
                        <el-col :span="11">
                            <el-input type="number" v-model="rows.limit_day" placeholder="填写0或者为空表示不限制">
                                <template #prepend>每</template>
                                <template #append>天</template>
                            </el-input>
                        </el-col>
                        <el-col :span="1">
                        </el-col>
                        <el-col :span="12">
                            <el-input type="number" v-model="rows.limit_num" placeholder="填写0或者为空表示不限制">
                                <template #prepend>限制使用</template>
                                <template #append>张</template>
                            </el-input>
                        </el-col>
                    </el-row>
                </el-form-item>
            </template>
        </yun-form>
    </el-card>
</template>
<script>
import form from "@components/Form.js";
export default{
    components:{
        'YunForm':form
    },
    data:{
        columns:[
            {field:"id",title:"ID",edit:"hidden"},
            {field:"title",title:"卡券名称",edit:"text",rules:"required"},
            {field:"description",title:"使用场景",edit:"text"},
            {field:"type",title:"卡券类型",edit:"select",rules:"required",searchList:Yunqi.data.couponTypeList},
            {field:"goods_id",title:"支持商品",edit:'selects',searchList: Yunqi.data.goods},
            {field:"discount",title:"折扣",edit:"number",rules:"range(0~10)",visible:function(row){return row.type == 'discount'}},
            {field:"cash",title:"代金券额度",edit:"number",rules:"range(0~)",visible:function(row){return row.type == 'cash'}},
            {field:"full_minus",title:"满减规则",edit:{form: 'fieldlist',label:['满','减']},visible:function(row){return row.type == 'full_minus'}},
            {field:"goods_number",title:"兑换数目",edit:"number",rules:"range(0~)",visible:function(row){return row.type == 'goods'}},
            {field:"balance",title:"充值金额",edit:"number",rules:"range(0~)",visible:function(row){return row.type == 'card'}},
            {field:"expire",title:"有效期",edit: {form:"input",type:"number",append:"天",placeholder:"填写0或者为空表示不限制"}},
            {field:"total",title:"发放总数",edit: {form:"input",type:"number",append:"张",placeholder:"填写0或者为空表示不限制"}},
            {field:"limit_day_number",title:"使用数量限制",edit:"slot"},
            {field:"use_limit_type",title:"使用日期限制",edit: 'select',searchList: {'':'不限制',week:'限制使用星期',date:'限制使用日期'}},
            {field:"use_limit_week",title:"限制使用星期",edit:"checkbox",searchList: {1:'星期一',2:'星期二',3:'星期三',4:'星期四',5:'星期五',6:'星期六',7:'星期日'},visible:function(row){return row.use_limit_type == 'week'}},
            {field:"use_limit_date",title:"限制使用日期",edit:"daterange",visible:function(row){return row.use_limit_type == 'date'}},
        ],
        row:''
    },
    onLoad:function (){
        let row=Yunqi.data.row || {
            limit_day:'',
            limit_num:'',
            type:'cash',
            full_minus:{'100':25,'200':60},
            use_limit_type:''
        };
        if(typeof row.full_minus=='string'){
            row.full_minus=JSON.parse(row.full_minus);
        }
        this.row=row;
    },
    methods: {
        onSubmit:function (row) {
            if(row.type=='cash' && !row.cash){
                Yunqi.message.error('代金券额度不能为空');
                this.$refs.yunform.setError('cash','代金券额度不能为空');
                return false;
            }
            if(row.type=='discount' && !row.discount){
                Yunqi.message.error('折扣不能为空');
                this.$refs.yunform.setError('discount','折扣不能为空');
                return false;
            }
            if(row.type=='full_minus' && Object.keys(row.full_minus).length==0){
                Yunqi.message.error('满减规则不能为空');
                this.$refs.yunform.setError('full_minus','满减规则不能为空');
                return false;
            }
            if(row.type=='goods'){
                let error='';
                let name='';
                if(!row.goods_id || row.goods_id.length==0){
                    error='商品券，商品不能为空';
                    name='goods_id';
                }else if(row.goods_id.indexOf(',')!=-1){
                    error='商品券，只能选择一种商品';
                    name='goods_id';
                }else if(!row.goods_number){
                    error='商品兑换数目不能为空';
                    name='goods_number';
                }
                if(error){
                    Yunqi.message.error(error);
                    this.$refs.yunform.setError(name,error);
                    return false;
                }
            }
            return true;
        }
    }
}
</script>
<style>

</style>